﻿@{
    ViewBag.Title = string.Format("{0}-{1}-移动", GetString("ParentName"), GetString("PowerName"));
    Layout = "~/Views/Shared/_TreeLayout.cshtml";
}
@inherits CMSWebViewPage<object>
<div id="toolbar_buttoninfo" class="datagrid-toolbar">
    @CurrentButtonTool.Create()
</div>
<div class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'center'">
        <ul id="tree_powerinfo" data-options="url:'/powerinfo/TreeJson/1',animate:true,lines:true,onCheck:SelectParent,checkbox:true,cascadeCheck:false"></ul>
    </div>
</div>
@section Bottom{
    <script type="text/javascript">
        var objBizTree = new BizTree("#tree_powerinfo");
        function Save() {
            var selectNodes = objBizTree.source.tree("getChecked");
            if (selectNodes.length == 0) {
                ShowMessage("请选择一项");
                return;
            }
            if (selectNodes.length > 1) {
                ShowMessage("只能选择一项");
                return;
            }
            var targetId = '@GetInt("powerId")';
            var parentId = selectNodes[0].Data.Id;
            if (targetId == parentId) {
                ShowMessage("无法移动自身");
                return;
            }
            var target = objBizTree.source.tree('find', targetId);
            var targetParentId = target.Data.ParentId;
            if (targetParentId == parentId) {
                ShowMessage("已经在" + selectNodes[0].Data.PowerName + "下，无须移动");
                return;
            }
            var targetIDPath = target.Data.IDPath;
            var parentIDPath = selectNodes[0].Data.IDPath;
            if (parentIDPath.indexOf(targetIDPath) >= 0) {
                ShowMessage("无法将父权限移动到子权限下");
                return;
            }

            var objAjaxEntity = new AjaxEntity();
            objAjaxEntity.url = "/PowerInfo/SaveMove";
            objAjaxEntity.data = { targetId: targetId, parentId: parentId };
            objAjaxEntity.executing = function () {
                ShowProcess();
                return true;
            };
            objAjaxEntity.executeSuccess = function (InvokeResult) {
                if (InvokeResult.ResultCode == "Success") {
                    ShowMessage(InvokeResult.ResultMessage);
                    var $tree = window.parent.objBizTree.source;
                    $tree.tree({
                        onLoadSuccess: function () {
                            var node = $tree.tree('find', targetId);
                            $tree.tree('expandTo', node.target);
                            $tree.tree('select', node.target);
                        }
                    });
                    Back();
                } else {
                    ShowMessage(InvokeResult.ResultMessage);
                }
                CloseProcess();
            };
            objAjaxEntity.execute();
        }
        function Back() {
            TransferToUrl("/powerinfo/powerinfoIndex?PowerName=@(GetString("ParentName"))");
        }
        //遍历取消选中，效率太低，UI也卡，放弃
        function SelectParent(node) {
            var selectNodes = objBizTree.source.tree("getChecked");
            if (selectNodes.length > 1) {
                ShowMessage("只能选择一项");
            }
        }
    </script>
}